package red.service.impl;

import net.sf.json.JSONArray;
import red.action.TextAction;
import red.model.ArticleModel;
import red.model.likeModel;
import red.service.TextService;
import red.util.MysqlConnectUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TextServiceImpl implements TextService {
    private MysqlConnectUtils mysqlConnectUtils = new MysqlConnectUtils();
    private java.sql.PreparedStatement preparedStatement;
    private ResultSet rs;

    @Override
    public boolean addText(ArticleModel text){
        String sql = "insert into article(article_id,user_name,headline,column_name,content,page,like_number,audit,publish_date,column_id,user_id)"
                +"value('"+text.getArticle_id()+"','"+text.getUser_name()+"','"+text.getHeadline()+"','"+text.getColumn_name()+"','"+text.getContent()+"','"+text.getPage()+"','"+text.getLike_number()+"','"+text.getAudit()+"',now(),'"+text.getColumn_id()+"','"+text.getUser_id()+"')";
        return mysqlConnectUtils.insert(sql);
    }

    @Override
    public List findTextid(String text_id) {
        MysqlConnectUtils conn =new MysqlConnectUtils();
        List list = new ArrayList();
        String sql = "select * from article where article_id='"+text_id+"'";
        try {
            conn.connection();
            preparedStatement = conn.conn.prepareStatement(sql);
            rs = preparedStatement.executeQuery();
            while (rs.next()) {
                ArticleModel recommendBean = new ArticleModel();
                recommendBean.setHeadline(rs.getString("headline"));
                recommendBean.setUser_name(rs.getString("user_name"));
                recommendBean.setContent(rs.getString("content"));
                recommendBean.setPublish_date(rs.getString("publish_date"));
                list.add(recommendBean);
            }
            JSONArray jsonObject=JSONArray.fromObject(list);
            if (jsonObject.size()>0) {
                TextAction textlist=new TextAction();
                return jsonObject;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    @Override
    public boolean updatepage(String page,String text_id) {
        String sql ="update article set page='"+page+"'where article_id='"+text_id+"'";
        return mysqlConnectUtils.update(sql);
    }

    @Override
    public boolean updatelike_num(String like_number, String text_id) {
        String sql ="update article set like_number='"+like_number+"'where article_id='"+text_id+"'";
        return mysqlConnectUtils.update(sql);
    }

    @Override
    public boolean updateaudit(String audit, String text_id) {
        String sql ="update article set audit='"+audit+"'where article_id='"+text_id+"'";
        return mysqlConnectUtils.update(sql);
    }

    @Override
    public boolean addlike(likeModel like) {
        String sql = "insert into test_info(article_id,user_name,like_easy)"
                +"value('"+like.getArticle_id()+"','"+like.getUser_name()+"','"+like.getLike_easy()+"')";
        return mysqlConnectUtils.insert(sql);
    }
    public List findlike(String text_id,String user_name) {
        MysqlConnectUtils conn =new MysqlConnectUtils();
        List list = new ArrayList();
        String sql = "select * from test_info where article_id='"+text_id+"' and user_name='"+user_name+"'";
        try {
            conn.connection();
            preparedStatement = conn.conn.prepareStatement(sql);
            rs = preparedStatement.executeQuery();
            while (rs.next()) {
                likeModel recommendBean = new likeModel();
                recommendBean.setArticle_id(rs.getString("article_id"));
                recommendBean.setUser_name(rs.getString("user_name"));
                recommendBean.setLike_easy(rs.getString("Like_easy"));
                list.add(recommendBean);
            }
            JSONArray jsonObject=JSONArray.fromObject(list);
            if (jsonObject.size()>0) {
                TextAction likelist=new TextAction();
                return jsonObject;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

}
